#include "ComponentScriptable.h"



#include "../OgreFrameWork.h"
#include "../Object.h"
#include "../GameManager.h"

#include "ComponentMovable.h"


ComponentScriptable::ComponentScriptable(ComponentManager* _parent, ComponentArguments _args):
Component(_parent, _args)
{
    //ctor

 if (_args.hasArgument("script"))
    {
        loadScript("../data/scripts/"+_args.getArgument<std::string>("script"));
        OgreFramework::getSingletonPtr()->m_pLog->logMessage("ComponentScriptable() : Loading LUA Script :"+ _args.getArgument<std::string>("script"));
    }



}

ComponentScriptable::~ComponentScriptable()
{
    //dtor
}



/** @brief registerNewComponent
  *
  * @todo: document this function
  */
void ComponentScriptable::registerNewComponent(std::string _newComponentName)
{

}

/** @brief update
  *
  * @todo: document this function
  */
void ComponentScriptable::update(double timeSinceLastFrame)
{

runUpdateInScript(timeSinceLastFrame);

}

/** @brief loadFromXML
  *
  * @todo: document this function
  */
void ComponentScriptable::loadFromXML(TiXmlElement* parentElement)
{

}

/** @brief saveToXML
  *
  * @todo: document this function
  */
void ComponentScriptable::saveToXML(TiXmlElement* parentElement)
{

}

/** @brief destroy
  *
  * @todo: document this function
  */
void ComponentScriptable::destroy()
{
//clearScripts();
runClearInScript();
}

/** @brief initialise
  *
  * @todo: document this function
  */
void ComponentScriptable::initialize()
{
lua_register(L,"log",log);
lua_register(L,"giveOrderToGoTo",giveOrderToGoTo);

luaL_dostring(L,std::string("name = '"+((Object*)m_parent)->getName()+"'").c_str());

runInitInScript();

}


///////////////////////////////////////////////////////////////////////////////////
///                             Lua Functions                                   ///
///////////////////////////////////////////////////////////////////////////////////

int ComponentScriptable::log(lua_State *LS)
{

    std::string message = lua_tostring(LS, -1);
    lua_pop(LS, 1);
    OgreFramework::getSingletonPtr()->m_pLog->logMessage(" Log from LUA : "+ message);
    return 0;

}

/** @brief giveOrderToGoTo
  *
  * @todo: document this function
  */
int ComponentScriptable::giveOrderToGoTo(lua_State *LS)
{

      std::string nameObject = (std::string)lua_tostring(LS, 1);

    Object* obj= GameManager::getInstance()->getObjectMgrPtr()->getObjectFromName(nameObject);

   if (obj->hasComponent("Movable"))
   {
       Ogre::Vector3 pos;

    pos.x = (float)lua_tonumber(LS, 2);


    pos.y=0;

    pos.z = (float)lua_tonumber(LS, 3);

       ((ComponentMovable*)obj->getComponent("Movable"))->goToOrder(pos);
   }

   return 0;
}



/** @brief getObjectNearWithComponent
  *
  * @todo: document this function
  */
int ComponentScriptable::getObjectNearWithComponent(lua_State *LS)
{

}

/** @brief getObjectPositionName
  *
  * @todo: document this function
  */
int ComponentScriptable::getObjectPositionName(lua_State *LS)
{

}

/** @brief getPosition
  *
  * @todo: document this function
  */
int ComponentScriptable::getPosition(lua_State *LS)
{
    /*
    lua_getglobal(LS,"objName");
    stringc objName = lua_tostring(LS, -1);
    lua_pop(LS, 1);

    DynamicObjectScriptable* tempObj = DynamicObjectsManager::getInstance()->getObjectByName(objName);

    if(tempObj)
    {
        vector3df pos = tempObj->getPosition();

        lua_pushnumber(LS,pos.X);
        lua_pushnumber(LS,pos.Y);
        lua_pushnumber(LS,pos.Z);
    }*/

    return 3;
}

/** @brief setPosition
  *
  * @todo: document this function
  */
int ComponentScriptable::setPosition(lua_State *LS)
{
/*
    float z = (float)lua_tonumber(LS, -1);
    lua_pop(LS, 1);

    float y = (float)lua_tonumber(LS, -1);
    lua_pop(LS, 1);

    float x = (float)lua_tonumber(LS, -1);
    lua_pop(LS, 1);


    lua_getglobal(LS,"objName");
    stringc objName = lua_tostring(LS, -1);
    lua_pop(LS, 1);

    DynamicObjectsManager::getInstance()->getObjectByName(objName)->setPosition(vector3df(x,y,z));
*/
    return 0;

}

